using LeaRun.Application.Busines.SystemManage;
using LeaRun.Application.Entity.SystemManage;
using LeaRun.Util;
using LeaRun.Util.WebControl;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web.Mvc;

namespace LeaRun.Application.Web.Areas.SystemManage.Controllers
{
	public class ExcelImportController : MvcControllerBase
	{
		private ExcelImportBLL excelimportbll = new ExcelImportBLL();
		[HttpGet]
		public ActionResult Index()
		{
			return base.View();
		}
		[HttpGet]
		public ActionResult Form()
		{
			return base.View();
		}
		[HttpGet]
		public ActionResult SetFieldForm()
		{
			return base.View();
		}
		[HttpGet]
		public ActionResult GetPageListJson(Pagination pagination, string queryJson)
		{
			Stopwatch stopwatch = CommonHelper.TimerStart();
			var data = new
			{
				rows = this.excelimportbll.GetPageList(pagination, queryJson),
				total = pagination.total,
				page = pagination.page,
				records = pagination.records,
				costtime = CommonHelper.TimerEnd(stopwatch)
			};
			return this.ToJsonResult(data);
		}
		[HttpGet]
		public ActionResult GetFormJson(string keyValue)
		{
			ExcelImportEntity entity = this.excelimportbll.GetEntity(keyValue);
			IEnumerable<ExcelImportFiledEntity> details = this.excelimportbll.GetDetails(keyValue);
			var data = new
			{
                templateInfo = entity,
				filedsInfo = details
			};
			return this.ToJsonResult(data);
		}
		[HttpGet]
		public ActionResult GetDetailsJson(string keyValue)
		{
			IEnumerable<ExcelImportFiledEntity> details = this.excelimportbll.GetDetails(keyValue);
			return this.ToJsonResult(details);
		}
		[AjaxOnly(false), HttpPost, ValidateAntiForgeryToken]
		public ActionResult RemoveForm(string keyValue)
		{
			this.excelimportbll.RemoveForm(keyValue);
			return this.Success("删除成功。");
		}
		[AjaxOnly(false), HttpPost, ValidateAntiForgeryToken]
		public ActionResult UpdateState(string keyValue, int F_EnabledMark)
		{
			this.excelimportbll.UpdateState(keyValue, F_EnabledMark);
			return this.Success("操作成功。");
		}
		[AjaxOnly(false), HttpPost, ValidateAntiForgeryToken]
		public ActionResult SaveForm(string keyValue, string templateInfo, string filedsInfo)
		{
            ExcelImportEntity excelImportEntity = LeaRun.Util.Json.ToObject<ExcelImportEntity>(templateInfo);
            List<ExcelImportFiledEntity> list = LeaRun.Util.Json.ToList<ExcelImportFiledEntity>(filedsInfo);
			this.excelimportbll.SaveForm(keyValue, excelImportEntity, list);
			return this.Success("操作成功。");
		}
	}
}
